package com.talkweb.osmharmony.views.overlay;

import com.talkweb.osmharmony.api.IGeoPoint;
import com.talkweb.osmharmony.views.MapView;
import com.talkweb.osmharmony.views.Projection;
import ohos.agp.components.element.Element;
import ohos.agp.render.Canvas;
import ohos.agp.utils.Point;
import ohos.agp.utils.Rect;
import ohos.multimodalinput.event.MmiPoint;
import ohos.multimodalinput.event.TouchEvent;


/**
 * {@link IconOverlay} is an icon placed at a particular
 * {@link IGeoPoint} on the {@link MapView}'s surface.
 * <p>
 * Inspired by {@link Marker} but without the Datafields and the pop-window support.
 * <p>
 * Created by k3b on 16.07.2015.
 */
public class IconOverlay extends Overlay {
    /**
     * Usual values in the (U,V) coordinates system of the icon image
     */
    public static final float ANCHOR_CENTER = 0.5f, ANCHOR_LEFT = 0.0f, ANCHOR_TOP = 0.0f, ANCHOR_RIGHT = 1.0f, ANCHOR_BOTTOM = 1.0f;

    /*attributes for standard features:*/
    protected Element mIcon = null;
    protected IGeoPoint mPosition = null;

    protected float mBearing = 0.0f;
    protected float mAnchorU = ANCHOR_CENTER, mAnchorV = ANCHOR_CENTER;
    protected float mAlpha = 1.0f; //opaque

    protected boolean mFlat = false; //billboard;

    protected Point mPositionPixels = new Point();

    /**
     * save to be called in non-gui-thread
     */
    public IconOverlay() {
    }

    /**
     * save to be called in non-gui-thread
     */
    public IconOverlay(IGeoPoint position, Element icon) {
        set(position, icon);
    }

    /**
     * Draw the icon.
     */
    @Override
    public void draw(Canvas canvas, Projection pj) {
        if (mIcon == null)
            return;
        if (mPosition == null)
            return;

        pj.toPixels(mPosition, mPositionPixels);
        int width = mIcon.getWidth();
        int height = mIcon.getHeight();
//        int width = mIcon.getIntrinsicWidth();
//        int height = mIcon.getIntrinsicHeight();
        Rect rect = new Rect(0, 0, width, height);
        rect.offset(-(int) (mAnchorU * width), -(int) (mAnchorV * height));
        mIcon.setBounds(rect);

        mIcon.setAlpha((int) (mAlpha * 255));

        float rotationOnScreen = (mFlat ? -mBearing : pj.getOrientation() - mBearing);
        drawAt(canvas, mIcon, mPositionPixels.getPointXToInt(), mPositionPixels.getPointYToInt(), false, rotationOnScreen);
    }

    public IGeoPoint getPosition() {
        return mPosition;
    }

    public IconOverlay set(IGeoPoint position, Element icon) {
        this.mPosition = position;
        this.mIcon = icon;
        return this;
    }

    public IconOverlay moveTo(final TouchEvent event, final MapView mapView) {
        final Projection pj = mapView.getProjection();
        MmiPoint point = event.getPointerPosition(event.getIndex());
        moveTo(pj.fromPixels((int) point.getX(), (int) point.getY()), mapView);
        return this;
    }

    public IconOverlay moveTo(final IGeoPoint position, final MapView mapView) {
        mPosition = position;
        mapView.invalidate();
        return this;
    }
}
